<?php
namespace Home\Controller;
class IndexController extends BaseController {
	
	public function __construct(){
		parent::__construct();
	}
  
	
	public function index(){
		$this->db = D("article");
		$count = $this->db->count();
		$page = $this->page($count);
		$list = $this->db->order("up_date desc")->limit($page->firstRow,$page->listRows)->select();
		$show = $page->show();
		$hot = getPD(2);
		$this->assign(array(
				'list'=>$list,
				'show'=>$show,
				'hot'=>$hot,
		));
		$this->display();
	}
	
	/**
	 * 栏目列表
	 */
	public function cate(){
		$catid =$_GET['catid'];
		$area = getArea($catid);
		$cate=S('cateinfo');
		$this->db = D('Admin/Moxing');
		$model = $this->db->getModel();
		$this->db = D($model[$cate[$catid]['model_id']]['table']);
		$sonid = cateSon($catid);
		if(empty($sonid)){
			$sonid = $catid;
		}
		$count = $this->db->where("cate_id in(".$sonid.")")->count();
		$page = $this->page($count);
		$list = $this->db->where("cate_id in(".$sonid.")")->order('up_date desc')->limit($page->firstRow,$page->listRows)->select();
		$show = $page->show();
		$hot = getPD(2,10,$catid);
		$this->assign(array(
				'area'=>$area,
				'list'=>$list,
				'show'=>$show,
				'catname'=>$cate[$catid]['name'],
				'hot'=>$hot,
		));
		$this->display();
	}
	/**
	 * 文章展示
	 */
    public function show(){
    	$catid = $_GET['catid'];
    	$id = $_GET['id'];
    	$area = getArea($catid);
    	$cate=S('cateinfo');
    	$this->db = D('Admin/Moxing');
		$model = $this->db->getModel();
		$this->db = D($model[$cate[$catid]['model_id']]['table']);
    	$data = $this->db->find($id);
    	
    	$data_n = $this->db->where("id>".$data['id'])->order("id asc")->limit(0,1)->select();
    	$data_p = $this->db->where("id<".$data['id'])->order("id desc")->limit(0,1)->select();
    	
    	$hot = getPD(2,10,$data['cate_id']);
    	
    	$this->assign(array(
    			'data'=>$data,
    			'data_p'=>$data_p[0],
    			'data_n'=>$data_n[0],
    			'hot'=>$hot,
    			'area'=>$area,
    			'catname'=>$cate[$catid]['name'],
    	));
    	$this->display();
    	
    }
    
    /**
     * 单页面
     */
    public function onePage(){
    	$this->db = D('Admin/Page');
    	$data = $this->db->find($_GET['id']);
    	$this->assign(array(
    			'data'=>$data,
    	));
    	$this->display('page');
    }
    
    
    /**
     * 搜索
     */
    public function search(){
    	if(!empty($_POST['search'])||!empty($_GET['search'])){
    		if(!empty($_POST['search'])){
    			$search = I('post.search');
    		}else{
    			$search = I('get.search');
    		}
    		
    		$this->db =D('search_data');
    		$where = "binary ucase(title) like concat('%',ucase('{$search}'),'%') or
    		binary ucase(description) like concat('%',ucase('{$search}'),'%')";
    		$count = $this->db->where($where)->count();
    		$page = $this->page($count);
    		$show = $page->show();
    		$list = $this->db->where($where)->order('data_id desc')->limit($page->firstRow,$page->listRows)->select();
    		$hot = getPD(2);
    		$this->assign(array(
    				'list'=>$list,
    				'show'=>$show,
    				'hot'=>$hot,
    				'search'=>$search,
    		));
    	}
    	$this->display();
    }
    
    /**
     * 关于
     */
	public function song(){
		layout(false);
		$this->db = D('process');
		$list = $this->db->order("p_date desc")->select();
		
		$this->db = D("adver_data");
		$work = $this->db->where("a_id=136")->select();
		$this->assign(array(
				'work'=>$work,
				'process'=>$list,
		));
		$this->display();
		
	}
	
	
	/**
	 * 保存留言
	 */
	public function message(){
		$result = false;
		if(!empty($_POST)){
			$this->db = D('message');
			$arr = $_POST;
			$arr['creat_date'] = date("Y-m-d H:i:s");
			$result = $this->db->add($arr);
		}
		if($result){
			$result = true;
		}
		echo $result;
		exit();
	}
}

